package ch08_Swing高级组件.combo;

import ch07_GUI编程.BaseFrame;
import ch07_GUI编程.ImageFactory;

import javax.swing.*;
import java.awt.*;
import java.util.Vector;

public class JComboBoxDemo {
	public static void main(String[] args) {
		BaseFrame f = new BaseFrame("JComboBox Demo");

		JComboBox box = new JComboBox(createModel());
		box.setRenderer(new CountryComboBoxCellRenderer());
		box.setLocation(10, 10);
		box.setSize(240,36);

		f.add(box);
		f.showMe();

	}
	//以向量形式生成列表数据
	static Vector createModel() {
		int n =4;

		String[] icons = {"","","",""};
		String[] countries = {"中国","美国","希腊","巴西"};
		String[] descs = {"五千年历史的国家","美利坚合众国","欧洲文明发源地","足球王国"};

		JLabel[] iconLabs = new JLabel[n];
		JLabel[] countryLabs = new JLabel[n];
		JLabel[] descLabs = new JLabel[n];
		JPanel[] panels = new JPanel[n];

		Vector model = new Vector();
		for(int i = 0;i<n;i++) {
			iconLabs[i] = new JLabel(ImageFactory.create(icons[i]));
			countryLabs[i] = new JLabel(countries[i]);
			descLabs[i] = new JLabel(descs[i]);

			panels[i] = new JPanel();
			panels[i].setLayout(new BorderLayout());
			panels[i].add(iconLabs[i],BorderLayout.WEST);
			panels[i].add(countryLabs[i],BorderLayout.CENTER);
			panels[i].add(descLabs[i],BorderLayout.EAST);
			model.add(panels[i]);
		}
		return model;
	}
}
